package com.feicheng.controller.qw;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.message.MqttInfo;
import com.feicheng.common.mqtt.MqttOutboundChannel;
import com.feicheng.common.mqtt.MsgFactory;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.task.Cache;
import com.feicheng.common.task.MjmdTask;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Amenjin;
import com.feicheng.persistence.entity.Aroomyyjl;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.mapper.AroomyyjlMapper;
import com.feicheng.persistence.service.IAmenjinService;
import com.feicheng.persistence.service.IClientService;
import com.feicheng.service.a.FDzService;
import com.feicheng.service.a.FJlService;
import com.feicheng.service.a.FXyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/admin/qw")
@Slf4j
public class QwController {

    public static class CMD {
        public static final String  CMD_ID = "CmdID";
        public static final String  CMD_CODE = "CmdCode";
        public static final String  CMD_STATUS = "CmdStatus";
        public static final String  CMD_PARAMS = "CmdParams";
        public static final String  CMD_STATUS_DESC = "CmdStatusDesc";
        public static final String  VIEW_ID = "ViewId";
        public static final String  UID = "UID";
        public static final String  UKEY = "UKey";
    }

    public static class QCODEACCESSVAL {
        public static final String CODEVAL = "CodeVal";
        public static final String SN = "SN";
        public static final String UID = "UID";
        public static final String STATUS = "Status";
        public static final String VIEWID = "ViewId";
        public static final String STORE_ID = "StoreId";
        public static final String CLIENT_ID = "ClientId";
        public static final String UKEY = "UKey";

    }


    @Autowired
    MjmdTask task;

    @Autowired
    Cache cache;

    @Autowired
    MqttInfo mqttInfo;

    @Autowired
    FXyService xyService;

    @Autowired
    FJlService jlService;

    @Autowired
    FDzService dzService;

    @Autowired
    IAmenjinService menjinService;


    @GetMapping("/initdailytask")
    public ResultInfo runDailyTask() {
        task.runDzDailyTask();
        return ResultInfo.success();
    }

    @GetMapping("/initcache")
    public ResultInfo initcache() {
        cache.init();
        return ResultInfo.success();
    }

    @PostMapping("/upRecord")
    public JSONObject uprecord(@RequestBody JSONObject p) {
        log.info(p.toJSONString());

        JSONObject result = new JSONObject();

        String cmd = p.getString("cmd");
        String sn = p.getString("sn");
        Integer jobnumber = p.getInteger("jobnumber");
        String token = p.getString("token");

        result.put("cmd", "upRecord");
        result.put("token", token);
        result.put("result", 0);
        result.put("errmsg", "");
        Amenjin mj = menjinService.getById(sn);

        if ("F1upRecord".equals(cmd)) {
            String idval = cache.getString(jobnumber.toString());
            if (idval.startsWith("CLI")) {
                JSONObject param = new JSONObject();
                param.put("clientid", idval);
                param.put("mjid", sn);
                param.put("roomid", mj.getRoomid());
                param.put("storeid", mj.getStoreid());
                xyService.sl(param);
            } else if (idval.startsWith("JL")) {
                JSONObject param = new JSONObject();
                param.put("jlid", idval);
                param.put("mjid", sn);
                param.put("roomid", mj.getRoomid());
                param.put("storeid", mj.getStoreid());
                jlService.sl(param);
            } else if (idval.startsWith("DZ")) {
                JSONObject param = new JSONObject();
                param.put("dzid", idval);
                param.put("mjid", sn);
                param.put("roomid", mj.getRoomid());
                param.put("storeid", mj.getStoreid());
                dzService.sl(param);
            }

        } else {
            return result;
        }
        
        return result;
    }

    @PostMapping("/mqtt")
    public JSONObject mqtt(@RequestBody JSONObject param) {
        if (null == param) {
            param = new JSONObject();
        }
        String cmd = param.getString("cmd");
        String sn = param.getString("sn");
        log.info(sn);
//        if ("login".equals(cmd)) {

            param.put("cmd", "login");
            param.put("url", mqttInfo.getServerUrl());
            param.put("user", mqttInfo.getUsername());
            param.put("pd", mqttInfo.getPassword());
            param.put("host", mqttInfo.getHost());
            param.put("port", mqttInfo.getPort());
            param.put("uploadTopic", sn+"U");
            param.put("downTopic", sn+"D");
            param.put("clientID", sn);
            param.put("keepalive", 5000);
//        }
        log.info(param.toJSONString());
        return param;
    }
}
